
// 2024/2/26: 首个版本
// 2024/4/12: 补上ToGeneralSample参数

using System;
using System.Linq;
using ASEva;
using SO = System.Reflection.ObfuscationAttribute;

namespace WalkerControlV1
{
    class WalkerControl : Sample
    {
		public bool RequestControl { get; set; } // Weather to request control / 是否请求控制
		public Double Heading { get; set; } // [deg] Heading of moving, 0 as north, CCW is positive / 行进朝向，0为正北，逆时针为正
		public Double Speed { get; set; } // [KPH] Moving speed, 0 is static / 移动速度，0为静止

        public WalkerControl()
        {
			RequestControl = false;
			Heading = 0;
			Speed = 0;
        }

        public static string Title
        {
            get
            {
                return "Request control,Heading of moving[deg],Moving speed[KPH]";
            }
        }

        public static string Protocol
        {
            get
            {
                return "walker-control-v1";
            }
        }

        public static string[] Protocols
        {
            get
            {
                return new String[] { "walker-control-v1" }; 
            }
        }

        public override string GetGeneralSampleProtocol()
        {
            return Protocol;
        }

        public override string[] GetGeneralSampleProtocols()
        {
            return Protocols;
        }

        public override GeneralSample ToGeneralSample(int channel)
        {
            var sample = new GeneralSample();
            sample.SetTime(this);
            sample.Protocol = Protocol;
            sample.Channel = channel;

            var data = new GeneralSampleValue[3];

            data[0] = new GeneralSampleValue(RequestControl ? 1 : 0);
            data[1] = new GeneralSampleValue(Heading);
            data[2] = new GeneralSampleValue(Speed);

            sample.Values = data.ToList();
            sample.NumberOfSignificants = data.Length;

            return sample;
        }

        public override bool FromGeneralSample(GeneralSample sample)
        {
            SetTime(sample);

            if (sample.Protocol == "walker-control-v1")
            {
                if (sample.Values.Count != 3) return false;

                RequestControl = sample.Values[0].IsNumber() ? (sample.Values[0].number == 1) : false;
                Heading = sample.Values[1].IsNumber() ? sample.Values[1].number : 0.0;
                Speed = sample.Values[2].IsNumber() ? sample.Values[2].number : 0.0;

                return true;
            }

            return false;
        }
    }
}